<template>
  <div class="op-container">
    <div class="filter-container">
      <k-button v-if="authorityHandle(30)" class="top-btn" value="添加箱子" @click.native="addBoxDialog" />
      <!-- <k-button class="top-btn" value="删除箱子" @click.native="deleteBox" /> -->
      <!-- <k-button class="top-btn" value="玩法介绍" @click.native="ruleGuideDialog" /> -->
    </div>
    <!--表格相关 -->
    <!--表格相关 -->
    <el-table
      :data="boxList"
      fit
      border
      highlight-current-row
      style="width: 100%;border-radius:8px;"
      :header-cell-style="{background:'#F6F5FA',color:'#4E4F62'}"
      @sort-change="sortChange"
    >
      <el-table-column min-width="100px" label="序号" type="index" align="center" />
      <el-table-column min-width="200px" label="箱子图标" align="center">
        <template slot-scope="scope">
          <el-image
            style="width: 110px; height: 80px"
            :src="scope.row.imgUrl"
            :preview-src-list="[scope.row.imgUrl]"
            fit="contain"
          />
        </template>
      </el-table-column>
      <el-table-column prop="boxName" min-width="120px" label="箱子名称" show-overflow-tooltip align="center" />
      <el-table-column prop="rebate" min-width="100px" label="箱子折扣" align="center" />
      <el-table-column prop="price" min-width="100px" label="箱子价格" sortable="custom" align="center" />
      <el-table-column prop="expectPrice" min-width="100px" label="期望价格" align="center" />
      <el-table-column prop="expectProfit" min-width="100px" label="期望利润" align="center" />
      <el-table-column min-width="200px" label="饰品展示缩略图" align="center">
        <template slot-scope="scope">
          <el-image
            style="width: 110px; height: 80px"
            :src="scope.row.goodsImgUrl"
            :preview-src-list="[scope.row.goodsImgUrl]"
            fit="contain"
          />
        </template>
      </el-table-column>
      <el-table-column prop="assembleTime" min-width="150px" label="配置时间" align="center" />
      <el-table-column min-width="300px" label="操作" align="center">
        <template slot-scope="scope">
          <div class="op-div">
            <k-button v-if="authorityHandle(30)" class="table-btn" value="修改" @click.native="addBoxUpdateDialog(scope.row)" />
            <k-button class="table-btn" value="配置饰品" @click.native="configBabyLib(scope.row.id,30)" />
            <k-button v-if="authorityHandle(30)" class="table-btn" value="删除" @click.native="deleteBox(scope.row.id)" />
          </div>
        </template>
      </el-table-column>
      <el-table-column min-width="200px" label="上架状态" align="center">
        <template slot-scope="scope">
          <el-switch
            :disabled="!authorityHandle(30)"
            v-model="scope.row.boxStatus"
            active-color="#3b425d"
            inactive-color="#E9EBF2"
            active-text="上架"
            inactive-text="下架"
            :active-value="1"
            :inactive-value="2"
            @change="boxOnlineState($event,scope)"
          />
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加箱子 addBox 弹窗 -->
    <k-dialog
      :show-hide.sync="addBox.showHide"
      :modal-append-to-body="false"
      :append-to-body="true"
      class="addBox-dialog"
      :title="textMap[dialogStatus]"
      :show-foot="false"
      :two-btn="true"
      :close-fun="handleCloseDialog"
      @click="saveBoxData"
    >
      <el-form :model="addBoxInfo" label-width="auto" class="form-modify">
        <el-form-item label="箱子名称：" prop="name">
          <el-input v-model="addBoxInfo.boxName" show-word-limit placeholder="请输入饰品名称" />
        </el-form-item>
        <el-form-item label="箱子图标：">
          <k-upload-file
            ref="upload1"
            v-model="addBoxInfo.imgUrl"
            upload-style="img"
            @successUpload="successUpload($event,0)"
          />
          <span style="color: #b2b2b2">只能上传jpg/png文件，且不超过2M</span>
        </el-form-item>
      </el-form>
    </k-dialog>

    <!-- addBox 修改弹窗 -->
    <k-dialog
      :show-hide.sync="addBoxUpdate.showHide"
      :modal-append-to-body="false"
      :append-to-body="true"
      class="addBox-dialog"
      title="修改箱子"
      :show-foot="false"
      :two-btn="true"
      @click="saveBoxData"
    >
      <el-form :model="addBoxInfo" label-width="auto" class="form-modify">
        <el-form-item label="箱子名称：" prop="name">
          <el-input v-model="addBoxInfo.boxName" show-word-limit placeholder="请输入箱子名称" />
        </el-form-item>
        <el-form-item label="箱子图标：" prop="iconUrl">
          <k-upload-file
            ref="upload1"
            v-model="addBoxInfo.imgUrl"
            upload-style="img"
            @successUpload="successUpload($event,0)"
          />
          <span style="color: #b2b2b2">只能上传jpg/png文件，且不超过2M</span>
        </el-form-item>
        <el-form-item label="饰品展示缩略图：" prop="iconUrl">
          <k-upload-file
            ref="upload2"
            v-model="addBoxInfo.goodsImgUrl"
            upload-style="img"
            @successUpload="successUpload($event,1)"
          />
          <span style="color: #b2b2b2">只能上传jpg/png文件，且不超过2M</span>
        </el-form-item>
      </el-form>
    </k-dialog>
    <!-- 玩法介绍 弹窗 -->
    <k-dialog
      :show-hide.sync="ruleGuide.showHide"
      :modal-append-to-body="false"
      :append-to-body="true"
      title="玩法介绍"
      class-name="ruleGuide-dialog"
      :show-foot="false"
      :two-btn="true"
      @click="setFissionBoxDescribe"
    >
      <div class="dialog-ruleGuide" style="padding:30px">
        <el-form :model="ruleGuideContent" label-width="auto">
          <el-form-item label="内容介绍：" prop="content">
            <quill-editor
              ref="myQuillEditor"
              v-model.trim="ruleGuideContent.content"
              :options="editorOption"
              class="editer"
            />
          </el-form-item>
        </el-form>
      </div>
    </k-dialog>
  </div>
</template>

<script>
import { updateBoxUpStatus, getFissionBoxDescribe, setFissionBoxDescribe, saveFissionData, deleteFissionData, listFissionData } from '@/api/rules/listdata'

export default {
  data() {
    return {
      editorOption: { //  富文本编辑器配置
        placeholder: '请输入内容',
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'], // 加粗，斜体，下划线，删除线
            ['blockquote', 'code-block'], // 引用，代码块
            [{ 'header': 1 }, { 'header': 2 }], // 标题，键值对的形式；1、2表示字体大小
            [{ 'list': 'ordered' }, { 'list': 'bullet' }], // 列表
            [{ 'script': 'sub' }, { 'script': 'super' }], // 上下标
            [{ 'indent': '-1' }, { 'indent': '+1' }], // 缩进
            [{ 'direction': 'rtl' }], // 文本方向
            [{ 'size': ['small', false, 'large', 'huge'] }], // 字体大小
            [{ 'header': [1, 2, 3, 4, 5, 6, false] }], // 几级标题
            [{ 'color': [] }, { 'background': [] }], // 字体颜色，字体背景颜色
            [{ 'font': [] }], // 字体
            [{ 'align': [] }], // 对齐方式
            ['clean'], // 清除字体样式
            ['image'] // 上传图片、上传视频
          ]
        }
      },
      ruleGuideContent: {
        content: ''
      },
      addBoxInfo: {
        id: '', // 保存箱子配置时必传",
        imgUrl: '', // 箱子图标",
        goodsImgUrl: '', // 饰品展示缩略图",
        boxName: '' // 箱子名称",
      },
      boxList: [],
      adminuserinfo: {
        username: '',
        password: '',
        nickname: ''
      },
      adminrules: {
        nickname: [{ required: true, message: '请输入昵称', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }]
      },
      timeValue: '',
      conditions: {
        keyword: null
      },
      addBox: {
        showHide: false
      },
      addBoxUpdate: {
        showHide: false
      },
      ruleGuide: {
        showHide: false
      },
      dialogStatus: 'create',
      textMap: {
        update: '修改箱子',
        create: '添加箱子'
      }
    }
  },
  created() {
    this.loadData()
    this.getFissionBoxDescribe()
  },
  methods: {
    // 保存玩法管理
    setFissionBoxDescribe() {
      const contents = this.ruleGuideContent.content
      setFissionBoxDescribe({ 'data': contents }).then(res => {
        this.ruleGuide.showHide = false
      })
    },
    // 加载玩法管理
    getFissionBoxDescribe() {
      getFissionBoxDescribe().then(res => {
        this.ruleGuideContent.content = res.data
      })
    },
    loadData() {
      listFissionData().then(res => {
        this.boxList = res.data
      })
    },
    // 表格的排序条件发生变化的时候会触发该事件
    sortChange(column, prop, order) {
      this.listQuery.sort = column.prop
      if (column.order === 'ascending') {
        this.listQuery.direction = 'asc'
      } else if (column.order === 'descending') {
        this.listQuery.direction = 'desc'
      }
      this.loadData()
    },
    // 删除箱子
    deleteBox(boxid) {
      this.$confirm('确定要删除该箱子么？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteFissionData({ 'id': boxid }).then(res => {
          if (res.data.success) {
            this.$message.success('操作成功')
            this.loadData()
          } else {
            this.$message.error(res.data.msg)
          }
        })
      }).catch(() => {

      })
    },
    // 箱子上架
    boxOnlineState(event, scope) {
      const id = scope.row.id
      updateBoxUpStatus({ 'id': id }).then(res => {
        this.$log(res)
      })
    },

    addBoxDialog() {
      this.$resetForm(this.addBoxInfo)
      this.addBox.showHide = true
    },
    addBoxUpdateDialog(row) {
      this.addBoxUpdate.showHide = true
      this.addBoxInfo.id = row.id
      this.addBoxInfo.boxName = row.boxName
      this.addBoxInfo.imgUrl = row.imgUrl
      this.addBoxInfo.goodsImgUrl = row.goodsImgUrl
      this.$log(this.addBoxInfo)
    },
    successUpload(data, flag) {
      this.$log(data + '----' + flag)
      if (flag === 0) {
        this.addBoxInfo.imgUrl = data
      } else {
        this.addBoxInfo.goodsImgUrl = data
      }
    },
    // 添加箱子
    saveBoxData() {
      saveFissionData(this.addBoxInfo).then(res => {
        if (res.data.success) {
          this.$message.success('保存成功')
          this.loadData()
        } else {
          this.$message.error(res.data.msg)
        }
        this.$resetForm(this.addBoxInfo)
        this.addBox.showHide = false
        this.addBoxUpdate.showHide = false
      })
    },
    handleCloseDialog() {
      this.$resetForm(this.addBoxInfo)
      this.addBox.showHide = false
    },
    ruleGuideDialog() {
      this.ruleGuide.showHide = true
    },
    // 跳转配置饰品
    configBabyLib(boxid,permissionid) {
      this.$router.push({ path: '/rootplayrules/config-babylibnew', query: { id: boxid, permissionid }})
    }
  }

}
</script>
<style lang="scss" scoped>
$mainColor:#3b425d;// 主色调
$disabledColor: #909399; //禁用色
$hoverColor:#1D143C;// 鼠标停留主色调浅色

.op-container{
  margin: 20px;
}

.filter-container {
  width: 100%;
  height: 48px;
  line-height: 48px;
  margin-bottom: 30px;
  .filter-item {
    display: inline-block;
    vertical-align: middle;
    margin-bottom: 10px;
  }
  .top-btn {
    margin-top: 10px;
    font-size: 14px;
    color: #ffffff;
  }
  .kInput {
    position: absolute;
    right: 20px;
  }
}
::v-deep .el-select .el-input.is-focus .el-input__inner {
    border-color: $mainColor;
}
::v-deep .el-input__inner:focus {
    border-color: $mainColor;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.price-input{
  width: 100px;
}
.addBox-dialog{
  ::v-deep .el-dialog__body{
    padding: 10px 80px;
  }

 ::v-deep .image-slot {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100%;
    background: #f5f7fa;
    color: #909399;
    font-size: 40px;
  }
}
.upload-btn {
  font-size: 14px;
  color: #ffffff;
}
.editer {
  border-radius: 8px;
  float: left;
  box-sizing: border-box;
  color: #606266;
  ::v-deep .ql-container {
    height: 440px;
    border-radius: 0px 0px 8px 8px;
  }
  ::v-deep .ql-toolbar.ql-snow {
    border-radius: 8px 8px 0px 0px;
  }
}
.table-btn{
  width: 90px;
  height: 35px;
  line-height:35px;
  padding: 0;
  background: #e7ecf8;
  border: 1px solid $mainColor;
  border-radius: 8px;
  font-size: 14px;
  color: $mainColor;
  &:hover{
    background: $mainColor;
    color: #fff;
  }
   &:focus {
     background-color: #e7ecf8;
  }
}

.form-modify{
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  margin: 10px 80px;
  .modify-right{
    margin-left: 80px;
  }
  ::v-deep .el-input__inner {
    // border-color: $mainColor;
    width: 200px;
  }
}

</style>
